package com.qcxk.yuqian.rtu.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.rtu.entity.Area;
import com.qcxk.yuqian.rtu.service.IAreaService;
import com.qcxk.yuqian.system.component.constant.SystemConstant;
import com.qcxk.yuqian.system.entity.Dept;
import com.qcxk.yuqian.system.utils.RedisUtil;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.List;


/**
 * @auther lp
 * @create 2021-11-15 03:16:56
 * @describe 前端控制器
 */
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IAreaService areaService;

    @BehaviourOperator(desc = "查询Area信息")
    @RequestMapping(value = "/loadArea")
    public JSONObject loadArea(Area area) {
        JSONObject json = new JSONObject();
        area = areaService.getById(area.getId());
        if (area != null) {
            json.put("code", "0");
            json.put("data", area);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询部门树")
    @RequestMapping(value = "/getArea")
    public JSONObject loadDept(Area area, String isContainUser) {
        JSONObject json = new JSONObject();
        if (area == null || area.getId() == null) {
            area = new Area();
            area.setId(1L);
        }
        List<Area> depts = areaService.getChildList("" + area.getId(), isContainUser);
        if (depts != null) {
            json.put("code", "0");
            json.put("data", depts);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询Area列表")
    @RequestMapping(value = "/queryAreaList")
    public JSONObject queryAreaList(Page page, Area area) {
        JSONObject json = new JSONObject();
        page = areaService.queryAreaPage(page, area);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());

        List<Area> list = page.getRecords();

        json.put("dataList", list);
        return json;
    }

    @BehaviourOperator(desc = "单一属性修改Area")
    @RequestMapping(value = "/updateAreaSingle")
    public JSONObject updateAreaSingle(Integer id, String column, String value) throws NoSuchFieldException, IllegalAccessException {
        JSONObject json = new JSONObject();
        Area area = areaService.getById(id);
        Class<? extends Area> clazz = area.getClass();// 获取到对象对应的class对象
        Field nameField = clazz.getDeclaredField(column);// 获取私有成员变量:column
        nameField.setAccessible(true);// 设置操作权限为true
        nameField.set(area, value);
        if (areaService.updateById(area)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "整体修改Area")
    @RequestMapping(value = "/updateAreaMultiple")
    public JSONObject updateAreaMultiple(Area area) {
        JSONObject json = new JSONObject();

        if (areaService.updateById(area)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除Area")
    @RequestMapping(value = "/deleteAreaById")
    public JSONObject deleteAreaById(Area area) {
        JSONObject json = new JSONObject();
        if (areaService.deleteAreaById(area)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "新增Area")
    @RequestMapping(value = "/addArea")
    public JSONObject addArea(Area area) {
        JSONObject json = new JSONObject();
        if (areaService.save(area)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

}

